package com.xr.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xr.constant.MessageConstant;
import com.xr.exception.CustomException;
import com.xr.mapper.CheckItemMapper;
import com.xr.pojo.CheckItem;
import com.xr.query.QueryPageBean;
import com.xr.result.PageResult;
import com.xr.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service(interfaceClass = CheckItemService.class)
public class CheckServiceImpl implements CheckItemService {

    @Autowired  //本地注解注入
    private CheckItemMapper checkItemMapper;

    //    测试项目搭建
    @Override
    public String test(String value) {
        System.out.println("服务提供者测试.....");
        return value;
    }

    //    查询所有检查列表info
    @Override
    public List<CheckItem> queryAllCheckItem() {
        return checkItemMapper.selectByExample(null);

    }
    // 修改检查项
    @Override
    public void updateCheckItem(CheckItem checkItem) {
        checkItemMapper.updateByPrimaryKey(checkItem);
    }

//    根據ID查詢檢查項
    @Override
    public CheckItem queryById(Integer id) {
        return checkItemMapper.selectByPrimaryKey(id);
    }

//    查詢所有檢查項
    @Override
    public List<CheckItem> queryAll() {
        return checkItemMapper.selectByExample(null);
    }

    //    分页+条件查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        /*
        * arg1:起始页
        * arg2:总页
        * */
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), true);
        Page<CheckItem> list = checkItemMapper.selectByQueryString(queryPageBean.getQueryString());
        return  new PageResult(list.getTotal(),list.getResult());
    }

    //    添加检查列表
    @Override
    public void insertCheckItem(CheckItem checkItem) {
        checkItemMapper.insertSelective(checkItem);
    }

    //    删除检查
    @Override
    public void deleteCheckItem(Integer id) throws CustomException {
        Integer count = checkItemMapper.isUseInCheckItemGroup(id);
        if(count>0){
//            被使用了则不能删除
            throw new CustomException(MessageConstant.CHECKITEM_IN_USE);
        }
        checkItemMapper.deleteByPrimaryKey(id);
    }
}
